/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-6-19 下午2:43:47
 */
package com.absir.bean.inject;

import java.lang.reflect.Method;

import com.absir.bean.basis.BeanFactory;
import com.absir.bean.core.BeanDefineMethod;
import com.absir.bean.inject.value.InjectType;
import com.absir.core.kernel.KernelClass;
import com.absir.core.kernel.KernelString;

/**
 * @author absir
 * 
 */
public class InjectMethod extends InjectInvoker {

	/** method */
	Method method;

	/** paramNames */
	String[] paramNames;

	/**
	 * @param method
	 * @param value
	 * @param injectType
	 */
	public InjectMethod(Method method, String value, InjectType injectType) {
		super(injectType);
		this.method = method;
		this.paramNames = BeanDefineMethod.getBeanParamNames(method);
		if (method.getParameterTypes().length == 1 && KernelString.isEmpty(paramNames[0])
				&& !KernelString.isEmpty(value)) {
			paramNames[0] = value;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.bean.inject.InjectInvoker#support(java.lang.Class)
	 */
	@Override
	public boolean support(Class<?> beanType) {
		// TODO Auto-generated method stub
		for (Class<?> type : method.getParameterTypes()) {
			if (beanType.isAssignableFrom(KernelClass.componentClass(type))) {
				return true;
			}
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.bean.inject.InjectInvoker#invoke(com.absir.bean.basis.BeanFactory
	 * , java.lang.Object)
	 */
	@Override
	public void invoke(BeanFactory beanFactory, Object beanObject) {
		// TODO Auto-generated method stub
		BeanDefineMethod.getBeanObject(beanFactory, beanObject, method, paramNames, injectType == InjectType.Required,
				injectType == InjectType.ObServed);
	}
}
